export default function(ele,duration=300){
    //IE10+/Android Browser4.4+/  
    let requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame   
    || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame   
    || function(clb){  
        return setTimeout(clb,1000/60);  
    };  

    let cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame   
        || window.webkitCancelAnimationFrame || window.msCancelAnimationFrame   
        || function(id){clearTimeout(id);};

    //距离滚动条最底部的距离
    let toBottom = 0
    if (ele == document) {
        let top = document.body.scrollTop || document.documentElement.scrollTop
        toBottom = (document.body.scrollHeight || document.documentElement.scrollHeight) - top
    }else {
        toBottom = ele.scrollHeight - ele.scrollTop
        // alert(ele.scrollHeight)
        // alert(ele.scrollTop)
        // alert(toBottom)
    }
    //计算步长  
    let step =  toBottom / (duration / ( 1000 / 60)) //取整
        
    //window.console && console.log(requestAnimationFrame);  
        
    function fn(){  

        if(toBottom >= 0){  
            toBottom -= step
            if (ele == document) {
                document.documentElement.scrollTop = document.body.scrollTop = (document.body.scrollHeight || document.documentElement.scrollHeight)-toBottom;  
            }else {
                ele.scrollTop = ele.scrollHeight -toBottom
            }
            
            fn.rafTimer = requestAnimationFrame(fn);  
        }else{
            // document.body.scrollTop = 0;  
            if (ele == document) {
                document.documentElement.scrollTop = document.body.scrollHeight || document.documentElement.scrollHeight 
            }else {
                ele.scrollTop = ele.scrollHeight
            }
            cancelAnimationFrame(fn.rafTimer);  
        }  
    }

    fn.rafTimer = requestAnimationFrame(fn);

}